package com.demo.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;

/**
 * Created by lcs on 2018/9/26.
 */

@Configuration
@MapperScan(basePackages = {"com.demo.**.dao","com.demo.**.mapper",})
public class DemoDaoConfig {

//	@Bean
//	public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws IOException {
//		SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
//		factory.setMapperLocations(
//				new PathMatchingResourcePatternResolver().getResources("classpath*:**/dao/*.xml")
//		);
//		factory.setDataSource(dataSource);
//		factory.setPlugins(new Interceptor[]{githubPageHelper()});
//		return factory;
//	}
//	@Bean
//	public DataSourceTransactionManager transactionManager(DataSource dataSource) {
//		return new DataSourceTransactionManager(dataSource);
//	}
//
//	@Bean
//	public JdbcTemplate jdbcTemplate(DataSource dataSource) {
//		return new JdbcTemplate(dataSource);
//	}
//
//	/**
//	 * new 一个 PageHelper
//	 * @author lcs
//	 * @return
//	 */
//	private PageHelper githubPageHelper(){
//		PageHelper pageHelper = new PageHelper();
//		Properties properties = new Properties();
//		// 和startPage中的pageNum效果一样
//		properties.setProperty("offsetAsPageNum","true");
//		//-- 设置为true时，使用RowBoun	ds分页会进行count查询
//		properties.setProperty("rowBoundsWithCount","true");
//		// （相当于没有执行分页查询，但是返回结果仍然是Page类型）
//		properties.setProperty("pageSizeZero","true");
//		// 禁用合理化时，如果pageNum<1或pageNum>pages会返回空数据
//		properties.setProperty("reasonable","false");
//		// 支持通过Mapper接口参数来传递分页参数
//		properties.setProperty("supportMethodsArguments","false");
//		// always总是返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page
//		properties.setProperty("returnPageInfo","none");
//
//		pageHelper.setProperties(properties);
//
//		return pageHelper;
//	}

	@Bean
	public PaginationInterceptor paginationInterceptor() {
		return new PaginationInterceptor();
	}

}
